package com.heimali.happyreader.utils;

import com.heimali.happyreader.HappyReaderApplication;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

public class NetUtils {
	private static ConnectivityManager cm = null;
	private static ConnectivityManager getConnectivityManager() {
		if (cm == null) {
			cm  = (ConnectivityManager) HappyReaderApplication.getApplication().getSystemService(Context.CONNECTIVITY_SERVICE);
		}
		return cm;
	}
	
	public static boolean isNetAvailable() {
		ConnectivityManager cm = getConnectivityManager();
		NetworkInfo info = cm.getActiveNetworkInfo();
		return (info != null && info.isConnected());
	}
	
	public static String getNetTypeName() {
		ConnectivityManager cm = getConnectivityManager();
		NetworkInfo info = cm.getActiveNetworkInfo();
		String typeName = "UNKNOWN";
		if (info != null && info.isConnected()) {
			int type = info.getType();
			if (type == ConnectivityManager.TYPE_MOBILE) {
				typeName = "MOBILE";
			} else if (type == ConnectivityManager.TYPE_WIFI) {
				typeName = "WIFI";
			}
		}
		return typeName;
	}
	
	/**
	 * 获取当前移动网络制式，如3G或2G(返回值参考：android.telephony.TelephonyManager.
	 * NETWORK_TYPE_XXX)
	 * 
	 * @return
	 */
	public static int getPhoneNetType() {
		TelephonyManager tm = (TelephonyManager)HappyReaderApplication.getApplication().getSystemService(Context.TELEPHONY_SERVICE);
		return tm.getNetworkType();
	}
}
